﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using System.Web.Mvc;
using Telerik.Web.Mvc.UI;

namespace MvcSimpleQuery.HtmlHelpers
{
    public static class TwitterBootstrap
    {
        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="TModel"></typeparam>
        /// <typeparam name="TProperty"></typeparam>
        /// <param name="htmlHelper"></param>
        /// <param name="expression"></param>
        /// <param name="selectList"></param>
        /// <param name="optionLabel"></param>
        /// <param name="justified"></param>
        /// <returns></returns>
        public static HtmlElement SplitButtonDropDown<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, IEnumerable<SelectListItem> selectList, string optionLabel,
            bool justified = false)
        {

            if (expression == null)
            {
                throw new ArgumentNullException("");
            }
            string exText = ExpressionHelper.GetExpressionText(expression);

            var _field = new HtmlElement("div").AddClass("btn-group")
            .AddChild<HtmlElement>(() =>
            {
                var dropButton = new HtmlElement("button").Attribute("type", "button").AddClass("btn", "btn-default").Text(exText);
                return justified ? new HtmlElement("div").AddClass("btn-group").AddChild<HtmlElement>(() => { return dropButton; }) : dropButton;
            })
            .AddChild<HtmlElement>(() =>
                {
                    var toggleButton = new HtmlElement("button").Attribute("type", "button").AddClass("btn", "btn-default", "dropdown-toggle").Attribute("data-toggle", "dropdown")
                 .AddChild<HtmlElement>(() =>
                 {
                     return new HtmlElement("span").AddClass("caret");
                 })
                 .AddChild<HtmlElement>(() =>
                 {
                     return new HtmlElement("span").AddClass("sr-only").Text("Toggle DropDown");
                 });
                    return justified ? new HtmlElement("div").AddClass("btn-group")
                        .AddChild<HtmlElement>(() => { return toggleButton; })
                        .AddChild<HtmlElement>(() =>
                        {
                            return _genDropDownMenu(selectList);
                        }) : toggleButton;
                });

            if (justified)
            {
                _field.AddChild<HtmlElement>(() =>
                {
                    return _genDropDownMenu(selectList);
                });
            }
            _field.AddChild<HtmlElement>(() =>
            {
                return new HtmlElement("input").Attribute("type", "hidden").Attribute("data-name", exText);
            });

            return justified ? _field.AddClass("btn-group-justified") : (_field);
        }

        public static HtmlElement SegmentedButton<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, IEnumerable<SelectListItem> selectList, string optionLabel)
        {
            if (expression == null)
            {
                throw new ArgumentNullException("");
            }
            string exText = ExpressionHelper.GetExpressionText(expression);

            var _field = new HtmlElement("div").AddClass("input-group").AddClass("autodrop")
            .AddChild<HtmlElement>(() =>
            {

                return new HtmlElement("input").Attribute("type", "text").AddClass("form-control").Attribute("placeholder", exText).Attribute("data-name", exText);
                //return new HtmlElement("button").Attribute("type", "button").AddClass("btn", "btn-default", "btn-sm").Text(exText)
            })
            .AddChild<HtmlElement>(() =>
              {
                  return new HtmlElement("div").AddClass("input-group-btn")
                    .AddChild<HtmlElement>(() =>
                  {
                      return new HtmlElement("button").Attribute("type", "button").AddClass("btn", "btn-default", "dropdown-toggle").Attribute("data-toggle", "dropdown")
                    .AddChild<HtmlElement>(() =>
                    {
                        return new HtmlElement("span").AddClass("caret");
                    });
                  })
                   .AddChild<HtmlElement>(() =>
                   {
                       return _genDropDownMenu(selectList);
                   });
              });
            return (_field);
        }


        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="TModel"></typeparam>
        /// <typeparam name="TProperty"></typeparam>
        /// <param name="htmlHelper"></param>
        /// <param name="expression"></param>
        /// <param name="selectList"></param>
        /// <param name="buttonContent"></param>
        /// <returns></returns>
        public static HtmlElement ButtonDropDown<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper
            , Expression<Func<TModel, TProperty>> expression, IEnumerable<SelectListItem> selectList
            , Action<HtmlElement> buttonContent
           
            )
        {
            if (expression == null)
            {
                throw new ArgumentNullException("");
            }
            string exText = ExpressionHelper.GetExpressionText(expression);

            var _conjunction = new HtmlElement("div").AddClass("btn-group");//.AddClass("btn-group-justified");
             
            return _ButtonDropDown(selectList, buttonContent, exText, _conjunction);
        }

        private static HtmlElement _ButtonDropDown(IEnumerable<SelectListItem> selectList, Action<HtmlElement> buttonContent, string exText, HtmlElement outer_div)
        {

            var button = new HtmlElement("button").AddClass("btn", "btn-default", "dropdown-toggle").Attribute("data-toggle", "dropdown");
         buttonContent(button);
            //should be an optional surrounding element
            return outer_div

                  .AddChild<HtmlElement>(() =>
                  {
                      return button;
                  })
                  .AddChild<HtmlElement>(() =>
                  {
                      return _genDropDownMenu(selectList);
                  })
                  .AddChild<HtmlElement>(() =>
                  {
                      return new HtmlElement("input").Attribute("type", "hidden").Attribute("data-name", exText);
                  });
        }

        private static HtmlElement _genDropDownMenu(IEnumerable<SelectListItem> selectList)
        {

            var ul = new HtmlElement("ul").AddClass("dropdown-menu");
            foreach (var con in selectList)
            {
                ul.AddChild<HtmlElement>(() =>
                {
                    return new HtmlElement("li")
                       .AddChild<HtmlElement>(() =>
                       {
                           return new HtmlElement("a").Attribute("href", "#").Text(con.Text);
                       });
                });
            }
            return ul;
        }


    }
}
